package chan.project.utils;

import org.apache.commons.lang3.ObjectUtils;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 处理ai生成的json数据格式问题
 */
public class AiChatUtil {
    public static void main(String[] args) {
        Long a = 0L;
        System.out.println(ObjectUtils.isNotEmpty(a));
    }
    public static String convertFormat(String chat){
        //考虑在多线程的条件下使用转换，应该使用原子类
        AtomicInteger count = new AtomicInteger(0);
        String chatFilter = chat.replaceAll("\\s", "");
        char[] charArray = chatFilter.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (char c : charArray) {
            if(c == '{'){
                count.incrementAndGet();
            }
            if(count.get() > 0){
                stringBuilder.append(c);
            }
            if(c == '}'){
                count.decrementAndGet();
                if(count.get() == 0){
                    return stringBuilder.toString();
                }
            }
        }
        return stringBuilder.toString();
    }
}
